<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>编辑用户</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/common/layuimini/layui.css" media="all">
  <link rel="stylesheet" href="/common/layuimini/public.css" media="all">
  <style>
    body {
      background-color: #ffffff;
    }
  </style>
</head>
<body>
<div class="layui-form layuimini-form">
  <input id="userId" class="userId" type="hidden" th:value="${userId}">
  <div class="layui-form-item">
    <label class="layui-form-label required">用户名</label>
    <div class="layui-input-block">
      <input type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
      <tip>填写登录账号</tip>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label required">性别</label>
    <div class="layui-input-block">
      <input type="radio" name="sex" value="1" title="男">
      <input type="radio" name="sex" value="0" title="女">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label required">手机</label>
    <div class="layui-input-block">
      <input type="number" name="phone" lay-verify="required" lay-reqtext="手机不能为空" placeholder="请输入手机" value="" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label required">邮箱</label>
    <div class="layui-input-block">
      <input type="email" name="email" lay-verify="required" lay-reqtext="邮箱不能为空" placeholder="请输入邮箱" value="" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label required">角色信息</label>
    <div class="layui-input-block">
      <div id="roleId" class="roleId"></div>
    </div>
  </div>

  <div class="layui-form-item" style="margin: 25px 10px;position:absolute;bottom: 0;right: 5%;">
    <div class="layui-input-block">
      <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
      <button type="button" class="layui-btn layui-btn-primary" onclick="javascript:parent.layer.close(parent.layer.getFrameIndex(window.name));">取消</button>
    </div>
  </div>
</div>
</body>
<script type="text/javascript" src="/common/layui/layui.js"></script>
<script type="text/javascript" src="/common/jquery-1.11.3.js"></script>
<script type="text/javascript" src="/common/xm-select/xm-select.js"></script>
<script type="text/javascript" src="/common/blog-ui.js"></script>
<script type="text/javascript">
  window.onload = function () {
    queryUser();
  }

  var demo2 = xmSelect.render({
    el: '#roleId',
    radio: true,
    clickClose: true, //点击之后关闭
    theme: {
      color: '#0081ff',
    },
    //查询下拉数据中包括全部，需要过滤掉全部数据，即第一个数据
    data: getRoleSelect().filter((val,index,arr)=>{
      return index !== 0;
    })
  })



  function queryUser(){
    $.ajax({
      url: "/user/admin/queryUser",
      type: "get",
      data: {
        userId: $("#userId").val()
      },
      success(res){
        if (res.state){
          setFormData(res.data);
        }
      },
      error(res){
        console.log(res);
      }
    })
  }

  /**
   * 获取角色的xm-select下拉选
   * @param _
   * @return
   */
  function getRoleSelect(){
    var data;
    $.ajax({
      url: "/user/admin/getRoleSelect",
      type: "get",
      async: false,
      data: {
        userId: $("#userId").val()
      },
      success(res){
        if (res.state){
          data = res.data;
        }
      },
      error(res){
        console.log(res);
      }
    })
    return data;
  }

  /**
   * 设置表单数据
   * @param data
   * @return
   */
  function setFormData(data){
    $("input[name='username']").val(data.username);
    $("input[name='phone']").val(data.phone);
    $("input[name='email']").val(data.email);
    $("input[name='sex'][value= \'"+data.sex+"\']").attr("checked","checked");
    layui.form.render();
  }

  /**
   * 保存用户
   * @param obj
   * @return
   */
  layui.form.on('submit(saveBtn)',function (obj){
    const data = {
      id: $("#userId").val(),
      username: obj.field.username,
      email: obj.field.email,
      sex: obj.field.sex,
      roleId: obj.field.select,
      phone: obj.field.phone
    }
    $.ajax({
      url: "/user/admin/updateUser",
      contentType: "application/json",
      type: "post",
      dataType: "json",
      data:JSON.stringify(data),
      success(res){
        if (res.state){
          parent.layui.table.reload('userTable');
          parent.layer.closeAll();
        }
      },
      error(res){
        console.log(res);
      }
    })

  })
</script>
</html>